package xin.marcher.module.quality.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.quality.domain.dto.ItemQualityControlSurveyDTO;
import xin.marcher.module.quality.domain.dto.ItemQualityControlSurveyResultDTO;
import xin.marcher.module.quality.domain.request.ItemQualityControlSurveyRequest;
import xin.marcher.module.quality.service.ItemQualityControlSurveyService;

import java.util.List;

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/quality/surveyPool")
public class ItemQualityControlSurveyController {

    @Autowired
    private ItemQualityControlSurveyService itemQualityControlSurveyService;

    /**
     * 调查池列表
     *
     * @param request
     * @return
     */
    @PreAuthorize("@rights.check('surveyPoolList', #token)")
    @PostMapping("/surveyPoolList")
    public JsonResult<List<ItemQualityControlSurveyDTO>> surveyPoolList(String token, @RequestBody ItemQualityControlSurveyRequest request) {
        try {
            return JsonResult.buildSuccess(itemQualityControlSurveyService.surveyPoolList(request));
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 加入调查池
     *
     * @param request
     * @return
     */
    @PreAuthorize("@rights.check('joinSurveyPool', #token)")
    @PostMapping("/joinSurveyPool")
    public JsonResult<ItemQualityControlSurveyResultDTO> joinSurveyPool(String token, @RequestBody ItemQualityControlSurveyRequest request) {
        try {
            return JsonResult.buildSuccess(itemQualityControlSurveyService.joinSurveyPool(request));
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
